# Maintainer: Christoph Reiter <reiter.christoph@gmail.com>

_realname=ngtcp2
pkgbase=mingw-w64-${_realname}
pkgname=("${MINGW_PACKAGE_PREFIX}-${_realname}")
pkgver=1.12.0
pkgrel=1
pkgdesc="An effort to implement IETF QUIC protocol (mingw-w64)"
arch=('any')
mingw_arch=('mingw32' 'mingw64' 'ucrt64' 'clang64' 'clangarm64')
msys2_repository_url='https://github.com/ngtcp2/ngtcp2'
url='https://nghttp2.org/ngtcp2/'
depends=("${MINGW_PACKAGE_PREFIX}-gcc-libs"
         "${MINGW_PACKAGE_PREFIX}-gnutls")
makedepends=(
  "${MINGW_PACKAGE_PREFIX}-cmake"
  "${MINGW_PACKAGE_PREFIX}-ninja"
  "${MINGW_PACKAGE_PREFIX}-cc"
)
license=('spdx:MIT')
source=("https://github.com/ngtcp2/ngtcp2/releases/download/v${pkgver}/ngtcp2-${pkgver}.tar.xz"{,.asc}
        "001-fix-pc-prefix.patch"
        "002-ngtcp2-pkgconfig-add-cflags-private.patch")
sha256sums=('2527a4c9305dbed610a000a88f94696526aa8959f74249a59f2b96ee73630629'
            'SKIP'
            'cdbb6fad1afc1eac3f3bca72cd83e49684439c02b0b74fd8841b9d1523ab86de'
            '0f80207af356ba9ca2de266ce3e46e5f52c91e23a2844526ea3592cf36a48326')
validpgpkeys=(
'F4F3B91474D1EB29889BD0EF7E8403D5D673C366' # Tatsuhiro Tsujikawa <tatsuhiro.t@gmail.com>
'516B622918D15C478AB1EA3A5339A2BE82E07DEC' # Tatsuhiro Tsujikawa <tatsuhiro.t@gmail.com>
)

prepare() {
  cd "${_realname}-${pkgver}"

  patch -Np1 -i "$srcdir/001-fix-pc-prefix.patch"
  patch -Np1 -i "$srcdir/002-ngtcp2-pkgconfig-add-cflags-private.patch"
}

build() {
  declare -a extra_config
  if check_option "debug" "n"; then
    extra_config+=("-DCMAKE_BUILD_TYPE=Release")
  else
    extra_config+=("-DCMAKE_BUILD_TYPE=Debug")
  fi

  mkdir -p "${srcdir}/build-shared-${MSYSTEM}" && cd "${srcdir}/build-shared-${MSYSTEM}"

  MSYS2_ARG_CONV_EXCL="-DCMAKE_INSTALL_PREFIX=" \
    "${MINGW_PREFIX}"/bin/cmake.exe \
      -GNinja \
      -DCMAKE_INSTALL_PREFIX="${MINGW_PREFIX}" \
      "${extra_config[@]}" \
      -DENABLE_SHARED_LIB=ON \
      -DENABLE_STATIC_LIB=OFF \
      -DENABLE_GNUTLS=ON \
      -DENABLE_OPENSSL=OFF \
      -DCMAKE_DLL_NAME_WITH_SOVERSION=ON \
      ../"${_realname}-${pkgver}"

  "${MINGW_PREFIX}"/bin/cmake.exe --build .

  mkdir -p "${srcdir}/build-static-${MSYSTEM}" && cd "${srcdir}/build-static-${MSYSTEM}"

  MSYS2_ARG_CONV_EXCL="-DCMAKE_INSTALL_PREFIX=" \
    "${MINGW_PREFIX}"/bin/cmake.exe \
      -GNinja \
      -DCMAKE_INSTALL_PREFIX="${MINGW_PREFIX}" \
      "${extra_config[@]}" \
      -DENABLE_SHARED_LIB=OFF \
      -DENABLE_STATIC_LIB=ON \
      -DENABLE_GNUTLS=ON \
      -DENABLE_OPENSSL=OFF \
      ../${_realname}-${pkgver}

  "${MINGW_PREFIX}"/bin/cmake.exe --build .
}

package() {
  cd "${srcdir}/build-static-${MSYSTEM}"
  DESTDIR="${pkgdir}" "${MINGW_PREFIX}"/bin/cmake.exe --install .

  cd "${srcdir}/build-shared-${MSYSTEM}"
  DESTDIR="${pkgdir}" "${MINGW_PREFIX}"/bin/cmake.exe --install .

  install -Dm644 "${srcdir}/${_realname}-${pkgver}/COPYING" "${pkgdir}${MINGW_PREFIX}/share/licenses/${_realname}/COPYING"
}
